package com.lanou.musicbox.fragment.seekmusic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.activity.VideoVIewActivity;
import com.lanou.musicbox.adapter.seekmusic.EnterTheSeekMusic_MVAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.MV;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/27.
 * 进入搜索_MV页面
 */
public class EnterTheSeekMusic_MVFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private List<MV.DataEntity> data;
    private ListView mvListView;
    private VolleySingleton volleySingleton;
    private EnterTheSeekMusic_MVAdapter adapter;
    private MV mv;
    private String newUrl;

    public static EnterTheSeekMusic_MVFragment passData(Context mContext, String str) {
        EnterTheSeekMusic_MVFragment fragment = new EnterTheSeekMusic_MVFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.entertheseekmusic_mv, null);
        mvListView = (ListView) view.findViewById(R.id.mvListView);
        mvListView.setCacheColorHint(1);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        mvListView.setOnItemClickListener(this);
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        newUrl = Contant.Url.URL_Seek_MV_first+ url + Contant.Url.URL_Seek_MV_end;
        initData();
        return view;
    }

    private void initData() {
        data = new ArrayList<>();
        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                 mv = gson.fromJson(response, MV.class);
                data = mv.getData();
                adapter = new EnterTheSeekMusic_MVAdapter(data, getActivity());
                mvListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MV.DataEntity dataEntity = (MV.DataEntity) parent.getItemAtPosition(position);
        String video_url = dataEntity.getMvList().get(0).getUrl();
        Intent intent = new Intent(getActivity(),VideoVIewActivity.class);
        intent.putExtra("video_url",video_url);
        startActivity(intent);
    }
}
